<template>
  <div style="margin-bottom: 40px">
    <div class="title">上传简历</div>
    <el-upload
      ref="my-upload"
      class="upload-demo"
      :action="action"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :on-success="handleSuccess"
      multiple
      :auto-upload="true"
      :limit="1"
      :on-exceed="handleExceed"
      :file-list="fileList"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <el-button size="small" v-if="dialogImageUrl" type="primary">点击预览</el-button>
    </el-upload>
    <el-dialog  :visible.sync="dialogVisible">
      <embed style="height:400px;width: 100%;" :src="dialogImageUrl" alt=""/>
    </el-dialog>
  </div>
</template>
<script>
import print from 'print-js'
import Config from '../../config'
import toolModel from '@/model/tool'
export default {
  data() {
    return {
      // // 上传文件参数
      // fileUpData: {}, // 文件上传附带参数
      // fileUpList: [], // 上传的文件列表
      // dialogFileUploadVisible: false,
      // upload_file_url: process.env.VUE_APP_ADMIN_BASE_URL + '/common/upload/upload_file', // 文件上传地址
      //   fileList: [{ name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }, { name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }],
      // file:null,
      action:Config.baseURL2+'/candidate/uploadFile',
      fileList: [],
      disabled: false, //
      dialogVisible: false, //预览框展示
      dialogImageUrl: '', //预览url
      //   header: {
      //     access_token: window.localStorage.getItem('access_token'),
      //   },
    }
  },
  methods: {
    handleRemove(file) {
      this.$refs['my-upload'].clearFiles()
      // this.fileList = []
      this.dialogImageUrl = ''
      // this.$refs['my-upload'].fileList.slice(0,-1)
    },
   async handlePictureCardPreview(file) {
     var that = this
      that.dialogVisible = true
      console.log(file,'file');
    //  that.dialogImageUrl  = 'http://192.168.1.9:8282/web2/candidate/downloadFile?filePath=简历.pdf'
    // var zz =  Config.baseURL2+'/candidate/downloadFile/'+file.response.data.filePath
     //console.log(xx,'xx');
    // this.dialogImageUrl  = Config.baseURL2+'/candidate/downloadFile/'+file.fileName
    // var zz  = await toolModel.preview({
    //   filePath:xx
    // })
    // console.log(zz,'zz')
     that.dialogImageUrl  = Config.baseURL2 + `/candidate/downloadFile?preview=true&filePath=${file.response.data.filePath}`
      // let res = await Config.baseURL2 + `/candidate/downloadFile?filePath=${file.response.data.filePath}`
     //let yy= URL.createObjectURL(zz)
    //   this.dialogImageUrl  = 'http://120.77.62.118/web/commontool/downloadFile/%E3%80%8A%E5%91%98%E5%B7%A5%E6%89%8B%E5%86%8CV%C2%A02022-01%E3%80%8B.pdf'
      // console.log(this.dialogImageUrl);
     // that.dialogImageUrl = require(yy)
    console.log(that.dialogImageUrl,666)
    },
    handleChange(file, fileList) {
      let imgSize = Number(file.size / 1024 / 1024)
      if (imgSize > 5) {
        this.$msgbox({
          title: '',
          message: '文件大小不能超过5MB，请重新上传。',
          type: 'warning',
        })
        this.fileList = fileList.slice(0, -1)
        return
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`,
      )
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleSuccess(file, fileList) {
      this.fileList = [fileList]
      console.log('上传成功', file, fileList,55)

    },
    // 文件下载
    handleDownload(file) {
      file.url = URL.createObjectURL(file.raw)
      if (!file.url) {
        this.$message.error('下载失败')
        return
      }
      document.location.href = file.url
      return
      const type = file.url.split('.')[4]
      // 判断文件类型
      if (type === 'doc' || type === 'docx' || type === 'xlsx' || type === 'xls' || type === 'ppt' || type === 'pptx') {
        // 在当前浏览器直接下载
        document.location.href = file.url
      } else {
        // 图片在浏览器打开 新的页面展示
        window.open(file.url, 'hello')
      }
    },
    // handleDownload(file){
    //     console.log('文件下载',file)
    // }
  },
}
</script>
<style lang="scss" scoped>
.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
    margin-bottom: 20px;
    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 40px;
  }

  .submit {
    float: left;
  }
}
</style>
